const express = require('express');
const router = express.Router();
// const { body, validationResult } = require('express-validator');
const issue = require('../api/Issue');


/**
 * 用于获取组织部门列表的接口
 * @route GET /issue/list
 * @group 组织架构
 * @summary GET 获取组织架构列表信息
 * @returns {object} SUCCESS -返回成功状态
 * @returns {object} msg -请求成功提示
 * @returns {object} data -请求的数据
 */

router.get('/issue/list',
    async (req, res) => {
        let result = await issue.getCaseIssueList();
        console.log(result)
        if (result) {
            res.send({
                "code": "SUCCESS",
                "msg": "获取数据成功",
                "data": result

            })
        } else {
            res.send("获取数据失败")
        }
    })


/**
 * 获取指定组织部门的所有下属级部门
 * @route GET /issue/list/sublist/ 组织架构成员
 * @group 组织架构
 * @summary 通过id获取指定的组织管理部门
 * @param {number} id.query.required -部门索引
 * @returns {object} SUCCESS -返回成功状态
 * @returns {object} msg -请求成功提示
 * @returns {Error}  default - Unexpected error
 */

router.get('/issue/list/sublist/', async (req, res) => {
    let id = req.query.id;
    let result = await issue.getCaseIssueFindId(id);
    if (result) {
        res.send({
            "code": "SUCCESS",
            "msg": "获取指定数据成功",
            "data": result
        })
    } else {
        res.send("获取数据失败")
    }
})

// swagegr注释
/**
 * @typedef IssueAdd
 * @property {integer} id
 * @property {string} name.required - Some description for Issue
 * @property {Array.<Point1>} Point
 */

/**
 * @typedef Point
 * @property {integer} parentId.required  
 * @property {string} name.required  
 */

/**
 * 创建组织管理部门下属级部门object
 * @route POST /issue/list/sublist/add 新增组织部门子部门
 * @param {Point.model} point.body.required 
 * @group 组织架构
 * @summary 创建新的组织管理子部门
 * @returns {object} SUCCESS -返回成功状态
 * @returns {object} msg -请求成功提示
 * @returns {object} data -请求的数据
 */

router.post('/issue/list/sublist/add', 
    async (req, res) => {
    let name = req.body.name;
    let parentId = req.body.parentId
    let result = await issue.createCaseIssue(name, parentId);
    if (result) {
        
        res.send({
            "code": "SUCCESS",
            "msg": "创建成功"
        })
    } else {
        res.send("创建失败")
    }
    })

// swagger注释
/**
 * @typedef IssueDelete
 * @property {integer} id
 * @property {string} name.required - Some description for Issue
 * @property {Array.<Point2>} Point2
 */

/**
 * @typedef Point2
 * @property {integer} parentId.required  
 * @property {integer} id.required  
 */
/**
 * 删除组织管理部门下属级部门
 * @route POST /issue/list/sublist/delete 删除组织部门子部门
 * @group 组织架构
 * @summary 删除组织管理子部门
 * @param {Point2.model} point.body.required 
 * @returns {object} SUCCESS -返回成功状态
 * @returns {object} msg -请求成功提示
 */
router.post('/issue/list/sublist/delete', async (req, res) => {
    let parentId = req.body.parentId;
    let id = req.body.id;
    let result = await issue.deleteCaseIssue(parentId, id);
    if (result) {
        res.send({
            "code": "SUCCESS",
            "msg": "删除成功",
        })
    } else {
        res.send("删除失败")
    }
})


// swagger注释
/**
 * @typedef IssueUpdate
 * @property {integer} id
 * @property {string} name.required - Some description for Issue
 * @property {Array.<Point3>} Point3
 */

/**
 * @typedef Point3
 * @property {integer} parentId.required  -组织管理部门索引
 * @property {integer} id.required  -要修改子部门索引
 * @property {string} name.required  -要修改后子部门名称
 */
/**
 * 修改组织管理部门下属级部门
 * @route POST /issue/list/sublist/update 修改组织部门子部门
 * @group 组织架构
 * @summary 修改组织管理子部门
 * @param {Point3.model} point.body.required 
 * @returns {object} SUCCESS -返回成功状态
 * @returns {object} msg -请求成功提示
 */

router.post('/issue/list/sublist/update', async (req, res) => {
    let parentId = req.body.parentId;
    let id = req.body.id;
    let name = req.body.name;
    let result = issue.updateCaseIssue(parentId, id, name);
    if (result) {
        res.send({
            "code": "SUCCESS",
            "msg": "修改成功"
        })
    } else {
        res.send("修改失败")
    }
})



module.exports = router;